<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-1.8.0.min.js"></script>
    <script src="../js/ajax.js"></script>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <link rel="stylesheet" href="../css/shousuokuan.css">
</head>

<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="sale">销售</a>
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm  layui-btn-danger" lay-filter="del" lay-event="del">删除</a>
</script>

<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">


                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-form-item">
                            <label class="layui-form-label">供货商</label>
                            <div class="layui-inline">
                                <select name="supplierId" id="supplierId" lay-search="">
                                    <option value=""></option>
                                </select>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">操作人</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="operatorName" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>

                        <!-- <label class="layui-form-label">起始时间</label>
                        <div class="layui-input-inline">
                            <input type="date" name="startDate" id="startDate" placeholder="请输入内容"
                                autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">最后时间</label>
                        <div class="layui-input-inline">
                            <input type="date" name="endDate" id="endDate" placeholder="请输入内容" autocomplete="off"
                                class="layui-input">
                        </div> -->

                        <div class="">
                            <label class="layui-form-label">时间：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startDate" id="startDate" class="layui-input" autocomplete="off"
                                       id="startDate" placeholder="开始时间">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="endDate" class="layui-input" id="endDate" autocomplete="off"
                                       placeholder="结束时间">
                            </div>
                            <div class="layui-input-inline">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>

                        </div>

                        <!-- <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div> -->

                    </div>
                </form>
            </div>
        </fieldset>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="add">添加数据</button>
    </div>
</script>
<table id="demo" lay-filter="test"></table>


<script src="../js/host.js"></script>
<script>
    $.ajaxSetup({
        headers: {
            "token": localStorage.getItem("token")
        }
    });

    layui.use(["table", 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#startDate'
        });
        laydate.render({
            elem: '#endDate'
        });
        var cou;

        var unitlist = [];
        new ajax(host + "/supplierMvc/getSupplier", "get", {}, "json", function (data) {
            unitlist = data;
            $.each(data, function (index, item) {
                $("#supplierId").append("<option value='" + item.id + "'>" + item.supplierName + "</option>")
            })
            form.render("select")
        });
        table.render({
            elem: '#demo'
            , url: host + '/GoodsInfo' //数据接口
            , page: true //开启分页,
            , headers: {
                "token": localStorage.getItem("token")
            }
            , toolbar: 'true'
            , toolbar: 'default'
            , toolbar: '#toolbarDemo'
            , loading: true
            , cols: [[ //表头
                // {type: 'checkbox', fixed: 'left'}
                 {type: 'numbers', title: '序号'}
                , {field: 'id', title: 'ID', sort: true, hide: true}
                , {field: 'commodityName', title: '商品名称'}
                // , {field: 'id', title: '商品编号', sort: true}

                , {
                    field: 'supplierId', title: '供货商', templet: function (d) {
                        console.log(d.LAY_INDEX); //得到序号。一般不常用
                        console.log(d.LAY_COL); //得到当前列表头配置信息（layui 2.6.8 新增）。一般不常用
                        //得到当前行数据，并拼接成自定义模板
                        for (var i = 0; i < unitlist.length; i++) {
                            if (d.supplierId == unitlist[i].id) {
                                return unitlist[i].supplierName;
                            }
                        }
                        return ""
                    }
                }
                , {field: 'price', title: '单价'}
                , {field: 'number', title: '数量'}
                , {field: 'totalPrice', title: '总价'}
                , {field: 'purchaseTime', title: '进货时间'}
                , {field: 'conclusion', title: '验收结论',templet:function(d){
                        if(d.conclusion==1)return '通过'
                        return '不通过'
                }}
                , {field: 'operatorName', title: '操作人编号'}
                , {fixed: 'right', width: 240, align: 'center', title: '操作', toolbar: '#barDemo'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2,
                        title: "添加数据",
                        content: "goodsOperator.html",
                        area: ['600px', '600px'],
                        end: function () {
                            table.reload('demo')
                        }
                    })
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        layer.alert('编辑 [id]：' + checkStatus.data[0].id);
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        console.log(data)
                        var arr = new Array()
                        for (var i of data) {
                            arr.push(i.id)
                        }
                        new ajax(host + "/delGoodsInfo", "get", {"arr": arr}, "json", function (data) {
                            layer.msg('删除成功');
                            table.reload('demo')
                        });
                    }
                    break;
            }
            ;
        });


        form.on("submit(add)", function (data) {
            layer.open({
                type: 2,
                title: "添加数据",
                content: "goodsOperator.html",
                area: ['600px', '600px'],
                end: function () {
                    table.reload('demo')
                }
            })
            return false;
        })
        form.on('submit(formDemo)', function (data) {
            //当前容器的全部表单字段，名值对形式：{name: value}】
            table.reload('demo', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始sss
                } //设定异步数据接口的额外参数
                //,height: 300
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        table.on("tool(test)", function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    var arr = [data.id]
                    $.post(host + "/delGoodsInfo", {"arr": arr}, function () {
                        obj.del();
                        layer.close(index);
                        table.reload('demo')
                    })
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '编辑数据',
                    shadeClose: true,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['600px', '600px'],
                    content: './goodsOperator.html',
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index)
                        body.find("#id").val(data.id)
                        body.find("#commodityName").val(data.commodityName)
                        body.find("#supplierId").val(data.supplierId)
                        body.find("#price").val(data.price)
                        body.find("#number").val(data.number)
                        body.find("#totalPrice").val(data.totalPrice)
                        body.find("#operatorName").val(data.operatorName)
                    },
                    end: function () {
                        table.reload('demo');
                    }
                });
            } else if (obj.event === 'sale') {
                layer.open({
                    type: 2,
                    title: '销售商品',
                    shadeClose: true,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['600px', '600px'],
                    content: './saleAdd.html',
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index)
                        body.find("#goodsId").val(data.id)
                        body.find("#goodsName").val(data.commodityName)
                    }
                });
            }
        })
    })
</script>
</body>

</html>